package logic

import (
	"anban/models"
	"anban/service"
	"anban/utils"
	"anban/utils/wechat"
	"fmt"
	"github.com/astaxie/beego"
	"time"
)

// 发送滞留校车通知
func SendAtBusNotice(student *models.Student, remark string) {
	var p map[string]interface{}
	classId := student.Class.Id
	familySn := student.FamilySn
	tplId := beego.AppConfig.String("atschoolbustplid")

	if student.School.IsUseMobileMsg == 1 {
		if len(familySn) > 0 {
			p = map[string]interface{}{}
			p["familySn"] =familySn
			p["status"] = 1
			_, userList := service.GetUserList(p)
			for _, user := range userList {
				if user.SubscribeStatus == 1 {
					wechat.SendSchoolBusNotice(user.OpenId, "请注意，您孩子没有下校车", remark, "", student.RealName, "无", "滞留校车中", time.Now().Format("2006-01-02 15:04:05"))
				}
				if len(user.Phone) > 1 {
					utils.SendMobileMessage([]string{user.Phone}, tplId, []string{user.RealName})
				}
			}
		}
	}

	if classId > 0 {
		p = map[string]interface{}{}
		p["teacherClassId"] = classId
		p["isMain"] = 1
		p["status"] = 1
		p["subscribeStatus"] = 1
		_, teacherList := service.GetUserList(p)
		for _, teacher := range teacherList {
			wechat.SendSchoolBusNotice(teacher.OpenId, "请注意，有学生没有下校车", remark, "", student.RealName, "无", "滞留校车中", time.Now().Format("2006-01-02 15:04:05"))
		}
	}

	return
}

// 发送校车刷卡通知
func SendSchoolBusNotice(student *models.Student, status string, remark string) {
	domain := beego.AppConfig.String("domain")
	path := beego.URLFor("parent.SchoolBusNoticeController.ShowList")
	wechatUrl := fmt.Sprintf("%s%s", domain, path)
	familySn := student.FamilySn

	if len(familySn) > 0 {
		p := map[string]interface{}{}
		p["familySn"] = familySn
		p["status"] = 1
		p["subscribeStatus"] = 1
		_, userList := service.GetUserList(p)
		for _, user := range userList {
			wechat.SendSchoolBusNotice(user.OpenId, "您有新的校车通知", remark, wechatUrl, student.RealName, "无", status, time.Now().Format("2006-01-02 15:04:05"))
		}
	}

	return
}
